package cc.mrbird.febs.cos.controller;


import cc.mrbird.febs.common.utils.R;
import cc.mrbird.febs.cos.entity.BiddingInfo;
import cc.mrbird.febs.cos.service.IBiddingInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author fanke
 */
@RestController
@RequestMapping("/cos/bidding-info")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BiddingInfoController {

    private final IBiddingInfoService biddingInfoService;

    SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 分页查询
     * @param page
     * @param biddingInfo
     * @return
     */
    @GetMapping("/page")
    public R getBooKInfoByPage(Page page, BiddingInfo biddingInfo) {
        return R.ok(biddingInfoService.page(page, Wrappers.<BiddingInfo>lambdaQuery()
                .gt(biddingInfo.getLast() != null && biddingInfo.getLast() == 1,BiddingInfo::getStartDate,sdf2.format(new Date()))
                .lt(biddingInfo.getLast() != null && biddingInfo.getLast() == 1,BiddingInfo::getEndDate,sdf2.format(new Date()))
                .like(biddingInfo.getTitle()!=null && biddingInfo.getTitle() != "",BiddingInfo::getTitle,biddingInfo.getTitle())));
    }

    /**
     * 新增
     * @param biddingInfo
     * @return
     */
    @PostMapping
    public R save(BiddingInfo biddingInfo) {
        biddingInfo.setAccessNum(0);
        biddingInfo.setCode(UUID.randomUUID().toString());
        return R.ok(biddingInfoService.save(biddingInfo));
    }

    /**
     * 修改
     * @param biddingInfo
     * @return
     */
    @PutMapping
    public R update(BiddingInfo biddingInfo) {
        return R.ok(biddingInfoService.updateById(biddingInfo));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public R deleteByIds(@PathVariable("ids") List<Integer> ids) {
        return R.ok(biddingInfoService.removeByIds(ids));
    }

}
